using System;
using System.Collections.Generic;
using System.Text;

namespace Com.Guocz.Media.FileSystem
{
    public class DataManager
    {
        private ChunkManager[] chunks;
        private Random r = new Random();

        public DataManager(MetaInfo meta)
        {
            ChunkManager male = ChunkManager.GetChunkManager(meta.DataMale);
            ChunkManager female = ChunkManager.GetChunkManager(meta.DataFemale);
            chunks = new ChunkManager[] { male, female };
        }

        public BlockMetaInfo write(byte[] data)
        {
            int chunkIndex = r.Next(DateTime.Now.Second) % 2;
            int blockIndex = chunks[chunkIndex].write(data);
            BlockMetaInfo meta = new BlockMetaInfo();
            meta.ChunkIndex = chunkIndex;
            meta.BlockIndex = blockIndex;

            return meta;
        }

        public byte[] read(BlockMetaInfo blockinfo)
        {
            return chunks[blockinfo.ChunkIndex].read(blockinfo.BlockIndex);
        }

        internal void Delete(BlockMetaInfo item)
        {
            chunks[item.ChunkIndex].Delete(item.BlockIndex);
        }
    }
}
